<%@page import="java.util.*"%>
<%@page import="java.text.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
    String path = request.getContextPath(); 
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+path+ "/"; 
%>
<!DOCTYPE HTML>
<html>
<head>
	<base href="<%=basePath%>">
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<title>消息列表</title>
	<link rel="stylesheet" href="plugin/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
	<link rel="stylesheet" href="css/bootstrap-datetimepicker.css" />
    <link rel="stylesheet" href="plugin/bootstrap-table/dist/bootstrap-table.css">
<style type="text/css">

ul{
    list-style-type: none;
}
 .breadcrumb{
          padding: 21px 25px;
       }  
 a {
    color: #2a3f54;
}        
</style>

</head>
<body>
    <div class="row base-margin" id="query">
        <ul class="breadcrumb">
				<li style="padding-left: 15px"><i class="icon-home home-icon"></i> <a href="javascript:void(0)">System Settings(系统设置)</a>
				</li>
				<li class="active">
				<a href="view/sys/formwork_list.jsp">Message Template(消息模板)</a></li>
        </ul>
        <form class="form-inline" role="form" style="float: left; width: 100%" method="post" id="queryForm">
            <button type="button" style="margin-left:35px" id="queryBtn" onclick="addOrUpdate(1)" class="btn btn-primary">Add(新建)</button>
            <button type="button" style="margin-left:15px" id="queryBtn" onclick="addOrUpdate(2)" class="btn btn-primary">Update(修改)</button>
            <button type="button" style="margin-left:15px" id="queryBtn" onclick="del()" class="btn btn-primary">Delete(删除)</button>
           <div class="form-group">  
                <label for="bName">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;模板名称:</label> 
                <input type="text" class="form-control input-append form_date"  id="formworkName" />
            </div>   
            <div class="form-group">
                <button type="button" id="queryBtn" onclick="doQuery1('demo-table',queryParams1());" class="btn btn-primary">搜索</button>
            </div> 
        </form>
    </div>
    <div class="container" style="width: 100%;margin-top:15px">
        <table id="demo-table">
        </table>
    </div>
</body>
	<!-- 加载需要的js -->
	<script src="js/jquery-3.2.1.min.js"></script>
	<script src="plugin/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
	<script src="js/jquery-bootstrap-pagination.js"></script>
	<script src="plugin/bootstrap-table/dist/bootstrap-table.min.js"></script>
	<script src="plugin/bootstrap-table/dist/locale/bootstrap-table-zh-CN.min.js"></script>
	<!--时间  -->
	<script src="js/bootstrap-datetimepicker.js"></script>
	<script src="js/bootstrap-datetimepicker.zh-CN.js"></script>
	<script src="js/common.js"></script>
	<script src="plugin/layer/layer.js"></script>
	<script type="text/javascript" charset="utf-8">
	$(function () {
		initPath("<%=basePath%>");
	    initTable('demo-table',queryParams,columns,"userMessageFormwork/getList");
	});
	
	   $('.form_date').datetimepicker({
	        minView: "month", //选择日期后，不会再跳转去选择时分秒 
	        language: 'zh-CN',
	        format: 'dd/mm/yyyy',
	        todayBtn:  1,
	        autoclose: 1,
	    });
var  columns= [ 
	 { field : 'formworkName',  title : '模板名称',  align : 'center', valign : 'middle'},
	 { field : 'mSummary',  title : '消息概述',  align : 'center', valign : 'middle' },
	 { field : 'mContext',  title : '内容',  align : 'center', valign : 'middle' },
	 { field : 'id',  title : '操作',  align : 'center', valign : 'middle',formatter:function(value,row,index){
			 return "<button type=\"button\"  onclick=\"send('"+value+"')\" class=\"btn btn-primary\">手动发送</button>";
		 } 
	  }
	 ];
	 //删除
function del(){
	var rows=$('#demo-table').bootstrapTable("getSelections");
	if(rows.length==0){
		layer.msg("请至少选择一条记录");
		return;
	}
	var arr = new Array();
	for(var i in rows){
		arr.push(row[i].id);	
	}
	var ids = arr.join(",");
	$.ajax({
        type: "get",
        url: "userMessageFormwork/del?ids="+ids,
        dataType:"json",
        success: function(data){    
       	  console.log(data)
      	  if(data.code==200){	
      		layer.msg("删除成功");
      		 window.location.reload(); 	
	        	 }else{
	        		  layer.msg(data.msg); 
	        	 }
   }
    });
	
}


	 //手动发送
function send(id){
	 var str=" <form class=\"form-inline\" role=\"form\" style=\"float: left; width: 100%\" method=\"post\" id=\"queryForm\">"
	        +" <p style='margin-left:20px'><span>批量发送</span> <input type='radio' id='identity1' onclick='doQuery1(\"tables\",queryParams3());'  value='1' name='identity' style='margin-left:10px'/>司机<input   onclick='doQuery1(\"tables\",queryParams3());' name='identity' type='radio' value='2' name='identity' style='margin-left:10px'/>用户<input type='radio' onclick='doQuery1(\"tables\",queryParams3());'  name='identity' id='identity3' value='3'  style='margin-left:10px'/>个人</p> "
	        +"<div class=\"form-group\" style='margin-left:20px'><label for=\"bName\">联系电话:</label> "
	        +"<input type=\"text\" class=\"form-control\"  id=\"telephone\" ><input type=\"hidden\"   id=\"identity\" >"
		    +"</div>   <div class=\"form-group\">  <button type=\"button\" id=\"queryBtn\""
		    +" onclick=\"doQuery1('tables',queryParams3())\" class=\"btn btn-primary\">搜索</button>"
		    +"</div>  "
		    +"</form><div class=\"container\" style=\"width: 100%;margin-top:15px\"> <table id=\"tables\">"
		    +" </table> </div>"
		    +"<div class=\"form-group\" style=\"padding-left: 35%\"><button  onclick=\"saveSend()\" class=\"btn btn-info\""
		    +" style=\"width: 200px\">确认</button> <button  onclick=\"cacle()\" class=\"btn btn-info\""
		    +" style=\"width: 200px\">取消</button></div>";
			layer.open({
				  type: 1,
				  title: "发送消息",
				  skin: 'layui-layer-rim', //加上边框
				  area: ['800px', '840px'], //宽高
				  content: str
				});
			var  columns1= [ { field: 'check', title: '全选', checkbox:true,formatter : stateFormatter },
				 { field : 'firstName',  title : '名称',  align : 'center', valign : 'middle' },
				 { field : 'telephone',  title : '联系电话',  align : 'center', valign : 'middle' },
			      ];//自定义方法，添加操作按钮 
			 initTable('tables',queryParams3,columns1,"user/getQueryList");
}
	 
	 function saveSend(){
		 var row=$('#tables').bootstrapTable("getSelections");
		 if(row.length==0){
			 layer.msg("请至少选中一条记录");
			 return;
		 }
		 var arr=new Array();
		 for(var i in row){
			 arr.push(row[i].id);
		 }
		var  ids=arr.join(",");
		 $.ajax({
	         type: "post",
	         url: "userMessageFormwork/sendMag",
	         data: JSON.stringify(data),
	         contentType:"application/json;charset=UTF-8", 
	         dataType:"json",
	         success: function(data){    	            	 
	        if(data.code==200){
	        	 window.location.reload();    
	  	       }else{
	  	    	    layer.msg(data.msg);
	  	       }
	                  }
	     }); 
	 }
	 
function stateFormatter(value, row, index) {
        return {
            checked : true//设置选中
        };
}
	 //checked选中
	 function attrs(obj){
		 var answer= document.getElementsByName("identity");
		 for (var i = 0; i < answer.length; i++) {//多个选项时
			 answer[i].checked = this.checked;
			 $('#identity').val(answer[i].value);
         }
	 }
	 //修改 新增
	function addOrUpdate(type){
	var formworkName="";var mSummary="";var mContext="";
	if(type==2){
		var row=$('#demo-table').bootstrapTable("getSelections");
		if(row.length==0){
			layer.msg("请选择一条记录，做修改");
			return;
		}
		if(row.length>1){
			layer.msg("请仅选择一条记录，做修改");
			return;
		}
		if(row.length==1){
			formworkName=row[0].formworkName==null?"":row[0].formworkName;
			mSummary=row[0].mSummary==null?"":row[0].mSummary;
			mContext = row[0].mContext==null?"":row[0].mContext;
		}
	}
			layer.open({
	 			  type: 1,
	 			  title: "消息模板",
	 			  skin: 'layui-layer-rim', //加上边框
	 			  area: ['520px', '660px'], //宽高
	 			  content: ' <form id="adds"><table>'+
	 			  '  <tr class="padtr"><td class="pad">模板名称:</td> '+
	 		       '<td><input type="text" class="form-control" name="formworkName" id="formworkName"   placeholder="请输入模板名称" value="'+formworkName+'"></td></tr>'+
	       '  <tr class="padtr"><td class="pad">消息标题:</td> '+
        '<td><input type="text" class="form-control" id="mSummary"  name="mSummary"   placeholder="请输入消息标题"  value="'+mSummary+'"></td></tr>'+
	         '  <tr class="padtr"><td class="pad">消息内容:</td> '+
        '<td><textarea  rows="3" cols="20" name="mContext">'+mContext+'</textarea></td></tr>'
        +
	        '<tr align="center" class="padtr"><td colspan="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
	        ' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
	        ' <button type="button" onclick="save()" class="btn btn-info btn1">&nbsp;保&nbsp;存&nbsp;&nbsp;</button>'+
	        '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
	        '<button type="button" onclick="cacel()" class="btn btn-info btn1">&nbsp;&nbsp;取&nbsp;消&nbsp;&nbsp;</button></td></tr></table></form>'
	 			});
}
	 
	 function save(){
		 var data = serialize('adds');//获取表单内容
		 if(data.formworkName==""){
			 layer.msg("模板名称不能为空");
			 return;
		}
		if(data.mSummary==""){
			 layer.msg("消息标题不能为空");
			 return;
		    }
		if(data.mContext==""){
			 layer.msg("消息内容不能为空");
			 return;
		    }
		console.log(data);
		 $.ajax({
	         type: "post",
	         url: "userMessageFormwork/addOrUpdate",
	         data: JSON.stringify(data),
	         contentType:"application/json;charset=UTF-8", 
	         dataType:"json",
	         success: function(data){    	            	 
	        if(data.code==200){
	        	 window.location.reload();    
	  	       }else{
	  	    	    layer.msg(data.msg);
	  	       }
	                  }
	     }); 
	 }
function cacel(){
	layer.closeAll();
}
function attrs(obj){
	
}
function queryParams3(){
	 var param = {
			 telephone:$('#telephone').val(), 
		        pageNo : this.pageNumber,
		        pageSize : this.pageSize,
		        identity:$("input[name='identity']:checked").val()
		    }	
		    return param;
}
	function queryParams(params) {
		 console.log(params);  
	    var param = {
    		startTime:$('#startTime').val(), 
    		endTime:$('#endTime').val(),
	        pageNo : this.pageNumber,
	        pageSize : this.pageSize
	    }	
	    return param;
	} 
	function queryParams1(){
		 var param = {
				startTime:$('#startTime').val(), 
		    	endTime:$('#endTime').val(),
		        pageNo : 1,
		        pageSize : this.pageSize
		    }	
		    return param;
	}
	</script>
</html>


